package com.android.lovely.ui.activity

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.core.widget.doAfterTextChanged
import com.android.lovely.LifecycleApplication
import com.android.lovely.R
import com.android.lovely.databinding.ActivityLoginBinding
import com.android.lovely.ext.fullScreen
import com.android.lovely.ext.loadBlurredImage
import com.android.lovely.framework.BaseMvvmActivity
import com.android.lovely.ui.dialog.UserDialog
import com.android.lovely.ui.viewmodel.LoginViewModel
import com.kotlin.extensions.context.ContextExt.startPage
import com.kotlin.extensions.keyboard.showKeyboard
import com.kotlin.extensions.view.ViewExt.clickable
import kotlinx.coroutines.flow.collectLatest

/**
 *
 * @author why
 * @date 2025/3/8 20:58
 */
class LoginActivity : BaseMvvmActivity<ActivityLoginBinding, LoginViewModel>() {
    override fun initView(savedInstanceState: Bundle?) {
        fullScreen()
        mBinding.ivBg.loadBlurredImage(R.drawable.bg_splash_login)
        mBinding.evPawword.doAfterTextChanged {
            updateLogin()
        }
        mBinding.tvGuest.isEnabled = true
        mBinding.tvLogin.isEnabled = false

        mBinding.evCount.doAfterTextChanged {
            updateLogin()
        }
        mBinding.ivEyes.clickable {
            togglePasswordVisibility()
        }
        mBinding.tvLogin.clickable {
            val email = mBinding.evCount.text.toString()
            val password = mBinding.evPawword.text.toString()
            mBinding.tvLogin.isEnabled = false
            viewModel.login(email, password) {
                mBinding.tvLogin.isEnabled = true
            }
        }
        mBinding.tvGuest.clickable {
            mBinding.tvGuest.isEnabled = false
            viewModel.guestLogin {
                mBinding.tvGuest.isEnabled = true
            }
        }
        mBinding.evCount.post {
            mBinding.evCount.requestFocus()
            mBinding.evCount.showKeyboard()
        }
        updateLogin()
    }

    override suspend fun initData() {
        viewModel.userLogin.collectLatest {it ->
            if (it.name.isEmpty()) {
                UserDialog().show(supportFragmentManager)
                return@collectLatest
            }
            startPage<HomeActivity>()
            LifecycleApplication.finishAll()
        }
    }

    private fun updateLogin() {
        val accountEnable = !mBinding.evCount.text.isNullOrBlank()
        val passwordEnable = !mBinding.evPawword.text.isNullOrBlank()
        mBinding.tvLogin.isEnabled = accountEnable && passwordEnable
    }

    private fun togglePasswordVisibility() {
        if (mBinding.ivEyes.isSelected) {
            mBinding.evPawword.transformationMethod = PasswordTransformationMethod.getInstance()
        } else {
            mBinding.evPawword.transformationMethod = HideReturnsTransformationMethod.getInstance()
        }

        mBinding.evPawword.setSelection(mBinding.evPawword.text?.length ?: 0)
        mBinding.ivEyes.isSelected = !mBinding.ivEyes.isSelected
    }
}